Skip to content

fix(session): handle Qoder requires_action idle state and SSE reconnection#54

Merged
heimanba merged 1 commit into
mainfrom
fix/session-run-tool-execution
Jul 22, 2026
Merged

fix(session): handle Qoder requires_action idle state and SSE reconnection#54
heimanba merged 1 commit into
mainfrom
fix/session-run-tool-execution

Conversation

@heimanba

Copy link
Copy Markdown
Contributor

Problem

agents session run produced no output (or hung indefinitely) when the Qoder agent needed to execute tools (e.g. Bash). The command would exit prematurely after receiving only thinking + tool_use events, missing the tool results and the final assistant reply.

Root Cause

Four interrelated issues:

  1. Mapper: requires_action treated as terminal — Qoder mapper mapped all session.status_idle events to status: "idle", which is a terminal status. But idle with stop_reason=requires_action means the agent paused for tool execution and will resume. Poll/stream exited on the first idle before tool results arrived.

  2. Stream: no SSE reconnectionstreamWithResume did a single yield* of the SSE stream. When Qoder closes the SSE connection after requires_action, the generator exited without reconnecting for subsequent events (tool_result, final message).

  3. SSE parser: id: field discardedBaseApiClient.sse() only parsed data: lines, ignoring the SSE id: field. This made event.id always undefined, so stream reconnection could not use after_id to skip already-consumed events.

  4. Polling: fixed 2s intervalcollectEventsUntilTerminal used a fixed 2-second polling interval, adding unnecessary latency for fast responses.

Fix

File Change
qoder/mapper.ts Map session.status_idle + requires_action"running" (non-terminal); extract raw.idevent.id
base-client.ts Parse SSE id: lines and inject into yielded objects
session-runtime.ts streamWithResume: reconnect loop with exponential backoff until terminal status; collectEventsUntilTerminal: exponential backoff (300ms → 2s) instead of fixed 2s
session-event-mappers.test.ts Test coverage for requires_action mapping

Performance

Scenario Before After
No tools (say hi) ~10s ~10s (unchanged)
Single tool (echo) stream hang / 107s 21s
Single tool (echo) polling no output / 80s 22s

Remaining latency is dominated by Qoder BYOC tool execution time (server-side), not client-side overhead.

…ction

When Qoder agents execute tools, the session transitions through
idle(requires_action) before resuming. Three issues caused session run
to produce no output or hang:

1. Qoder mapper treated session.status_idle with stop_reason
   requires_action as terminal idle, causing poll/stream to exit before
   tool results and the final reply arrived. Fixed by mapping it to
   running.

2. streamWithResume did a single yield* of the SSE stream. When Qoder
   closes the SSE connection after requires_action, the generator exited
   without reconnecting. Added a reconnect loop with exponential backoff
   that continues until a true terminal status event arrives.

3. SSE parser in base-client discarded the id: field from SSE frames,
   so event.id was always undefined and stream reconnection could not
   skip already-consumed events. Now extracts id: and injects it into
   the parsed payload.

Additionally, polling in collectEventsUntilTerminal now uses exponential
backoff (300ms initial, doubling to 2s cap) instead of a fixed 2s
interval, reducing first-response latency.

Change-Id: Ieefe7ad827516f92fcfff3023600564d9eff0eec
Co-developed-by: OpenCode <noreply@opencode.ai>
@heimanba
heimanba merged commit 2486412 into main Jul 22, 2026
14 checks passed
@heimanba
heimanba deleted the fix/session-run-tool-execution branch July 22, 2026 01:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant